<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class mmodulesposition extends MY_Model {

	var $theme_data = array(
		'layout_main' => 'layout',
		'left' => 'include/leftcp',
	);
	public $mytable = "modules_position";
	public $primaryKey = "MPOS_ID";
	public $resCode = "MODULES_POSITION";

	function __construct() {
		parent::__construct();
	}

	function field() {
		$fieldData = array();
		$fieldData['MOD_ID'] = 0;
		$fieldData['MPOS_TITLE'] = '';
		$fieldData['MPOS_CONFIG'] = '';
		$fieldData['MPOS_POSITION'] = '';
		$fieldData['MPOS_ORDER'] = '';
		$fieldData['MPOS_LAYOUT'] = '';
		return $fieldData;
	}
	
	function get_module($widget_id) {
		$widget_id = (int) $widget_id;
		if(!$widget_id) 
			return null;
		
		$widget = $this->get($widget_id);
		if (!$widget) 
			return null;
			
		$module = $this->mmodules->get($widget->MOD_ID);
		return $module;
	}
	
	
	public function gets_widget($position = '', $layout = '') {
		$this->db->order_by("MPOS_ORDER asc");
		if($position !='')
		$this->db->where("MPOS_POSITION",$position);
		if($layout !='')
		$this->db->where("MPOS_LAYOUT",$layout);
		$this->db->from($this->mytable . ' as mp');
		
		$this->db->join('modules as m', 'mp.MOD_ID = m.MOD_ID');
		$widgets = $this->db->get()->result();
		$result = array();
		if ($widgets)
		foreach ($widgets as $widget) {
			if (!isset($result[$widget->MPOS_POSITION]))
			$result[$widget->MPOS_POSITION] = array();
			unset($widget->MPOS_CONFIG);
			$result[$widget->MPOS_POSITION][] = $widget;
		}
		return $result;
	}
	
}

?>